package com.futures.investm.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.aries.ui.view.title.TitleBarView;
import com.futures.investm.R;
import com.futures.investm.activity.NewsActivity;
import com.futures.investm.adapter.ForeignAdapter;
import com.futures.investm.adapter.MarketMetalAdapter;
import com.futures.investm.adapter.NewsHomeAdapter;
import com.futures.investm.constant.Api;
import com.futures.investm.entity.ForeignEntity;
import com.futures.investm.entity.MarketInfo;
import com.futures.investm.entity.RealTimeInfo;
import com.futures.investm.utils.ListToStringUtil;
import com.futures.investm.weight.AutofitViewPager;
import com.futures.investm.weight.WrapContentHeightViewPager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.request.GetRequest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import tech.com.commoncore.base.BaseTitleFragment;
import tech.com.commoncore.entity.BaseTResp;
import tech.com.commoncore.snaphelper.GallerySnapHelper;

public class HomeNewFragment extends BaseTitleFragment {
//    private final String[] mTitles = { "精选", "科技", "新经济", "政策"};
//    private final String[] mTitles = {"24小时", "精选", "科技", "新经济", "政策"};
    @BindView(R.id.rv_contentFastLib)
    RecyclerView recyclerView;
    @BindView(R.id.rv_xing)
    RecyclerView rv_xing;
    ForeignAdapter mForeignAdapter;

    //精选
    @BindView(R.id.rv_jinxuan)
    RecyclerView rv_jinxuan;
    MarketMetalAdapter mMarketMetalAdapter;
//    private ArrayList<Fragment> mFragments = new ArrayList<>();
//    private MyPagerAdapter mAdapter;
//    @BindView(R.id.tl_4)
//    TabLayout tabLayout_4;
//    @BindView(R.id.vp)
//    WrapContentHeightViewPager vp;

    @BindView(R.id.ll_kuaixun)
    LinearLayout ll_kuaixun;

    @BindView(R.id.sm)
    SmartRefreshLayout sm;


    LinearLayoutManager newLayoutManager;
    LinearLayoutManager xingLayoutManager;
    GallerySnapHelper mNewsGallerySnapHelper;

    NewsHomeAdapter mNewsAdapter;

    public static HomeNewFragment newInstance() {
        Bundle bundle = new Bundle();
        HomeNewFragment fragment = new HomeNewFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void setTitleBar(TitleBarView titleBar) {

        titleBar
                .setLeftText("首页   ")
                .setDividerVisible(false)
                .setLeftTextDrawable(null)
                .setTitleMainTextColor(getResources().getColor(R.color.colorTitleText))
                .setLeftTextColor(getResources().getColor(R.color.colorTitleText))
                .setOnRightTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        IntentUtils.goMessageActivity(mContext);
                    }
                });


    }


    @Override
    public int getContentLayout() {
        return R.layout.fragment_home_new;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        newLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        //新闻
        recyclerView.setLayoutManager(newLayoutManager);
        mNewsAdapter = new NewsHomeAdapter();
        recyclerView.setAdapter(mNewsAdapter);
        mNewsGallerySnapHelper = new GallerySnapHelper();
        mNewsGallerySnapHelper.attachToRecyclerView(recyclerView);




        xingLayoutManager = new GridLayoutManager(mContext, 2);
        rv_xing.setLayoutManager(xingLayoutManager);
//        rv_xing.addItemDecoration(new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL));
        mForeignAdapter = new ForeignAdapter();

//        for (int i = 0; i < mTitles.length; i++) {
//
//            mFragments.add(ContentFragment.newInstance(i));
//
//        }
//        mAdapter = new MyPagerAdapter(getChildFragmentManager());
//        vp.setAdapter(mAdapter);
//        tabLayout_4.setupWithViewPager(vp);
//        vp.setOffscreenPageLimit(5); //预加载
//        vp.setCurrentItem(0);


        //贵金属
        rv_jinxuan.setLayoutManager(new LinearLayoutManager(mContext));
        mMarketMetalAdapter = new MarketMetalAdapter();



        ll_kuaixun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,NewsActivity.class);
                startActivity(intent);
            }
        });

        getMetal();

        getNews();

        getForeignDatas();

        sm.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {

                getMetal();

                getNews();

                getForeignDatas();

                sm.finishRefresh();
            }
        });

    }

    /**
     * 贵金属
     */
    private void getMetal() {
        //获取需要请求的贵金属参数
        List<String> list = Arrays.asList(mContext.getResources().getStringArray(R.array.MarketParam));

        GetRequest request = ViseHttp.GET(Api.MARKET_URL)
                .addParam("symbol", ListToStringUtil.listToString(list))
                .addParam("type", "2");
        request.request(new ACallback<BaseTResp<ArrayList<MarketInfo>>>() {
            @Override
            public void onSuccess(BaseTResp<ArrayList<MarketInfo>> data) {

                mMarketMetalAdapter.setNewData(data.data);

            }

            @Override
            public void onFail(int errCode, String errMsg) {
            }
        });
        rv_jinxuan.setAdapter(mMarketMetalAdapter);
    }

    private void getForeignDatas() {
        ViseHttp.GET(Api.FOREIGN_URL)
                .addParam("cid", "162")
                .request(new ACallback<BaseTResp<ArrayList<ForeignEntity>>>() {
            @Override
            public void onSuccess(BaseTResp<ArrayList<ForeignEntity>> data) {
                if (1 == data.code) {
                    if (data.data.size()>=4){
                        mForeignAdapter.replaceData(data.data.subList(0,4));
                    }else {
                        mForeignAdapter.replaceData(data.data);
                    }

                } else {
//                            ToastUtils.showToast("请求数据失败");
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {

            }
        });
        rv_xing.setAdapter(mForeignAdapter);
    }


    @Override
    public boolean isRegisterEvent() {
        return true;
    }

    private void getNews() {
        GetRequest request = ViseHttp.GET(Api.REAL_TIME_SCAN)
                .addParam("cid", "2")
                .addParam("limit", "20");
        request.request(new ACallback<BaseTResp<ArrayList<RealTimeInfo>>>() {
            @Override
            public void onSuccess(BaseTResp<ArrayList<RealTimeInfo>> data) {
                mNewsAdapter.setNewData(data.data.subList(7, 13));
            }

            @Override
            public void onFail(int errCode, String errMsg) {
            }
        });
        recyclerView.setAdapter(mNewsAdapter);
    }


//    @OnClick({R.id.img_exchange_notice, R.id.img_zjlx, R.id.img_exchange, R.id.img_qihuo_baike})
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.img_exchange_notice:
//                IntentUtils.goMsg(mContext);
//                break;
//            case R.id.img_zjlx:
//                IntentUtils.goFundActivity(mContext);
//                break;
//            case R.id.img_exchange:
//                IntentUtils.goExchangeActivity(mContext);
//                break;
//            case R.id.img_qihuo_baike:
//                IntentUtils.goArticleActivity(mContext);
//                break;
//        }
//    }

//    class MyPagerAdapter extends FragmentPagerAdapter {
//        public MyPagerAdapter(FragmentManager fm) {
//            super(fm);
//        }
//
//        @Override
//        public int getCount() {
//            return mFragments.size();
//        }
//
//        @Override
//        public CharSequence getPageTitle(int position) {
//            return mTitles[position];
//        }
//
//        @Override
//        public Fragment getItem(int position) {
//            return mFragments.get(position);
//        }
//    }

}










